﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace HyperV
{
    public class Guest
    {
        private string name;
        private Host host;

        internal Guest(string name, Host host)
        {
            this.name = name;
            this.host = host;
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public string Description
        {
            get
            {
                ManagementObject vm = GetVmObject();
                return vm["Description"].ToString();
            }
        }

        public string EnabledState
        {
            get
            {
                ManagementObject vm = GetVmObject();
                return vm["EnabledState"].ToString();
            }
        }

        private ManagementObject GetVmObject()
        {
            ConnectionOptions connOpts = new ConnectionOptions();
            connOpts.Username = host.Credentials.Username;
            connOpts.Authority = "ntlmdomain:" + host.Credentials.Domain;
            connOpts.SecurePassword = host.Credentials.Password;
            ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\root\virtualization", host.Address), connOpts);
            ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName=" + name);
            ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj);
            ManagementObjectCollection vmCollection = vmSearcher.Get();
            ManagementObject vm = (ManagementObject)vmCollection.GetEnumerator().Current;
            return vm;
        }
    }
}
